package com.daydr3am.thenation;

import org.w3c.dom.Node;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.FrameLayout.LayoutParams;

import com.daydr3am.lib.ImageLoader;
import com.eightbitcloud.pagingscroller.Pager;

public class Content extends SubScroll {
	public Boolean hilight=false;
	public int [] colorCode;
	int current;
	int hilightCall;
	View hilightPage;
	Node[] tmpList;
	int index;
	
    public Content(Context ctx, AttributeSet attrs) {
		super(ctx, attrs);
		// TODO Auto-generated constructor stub

        contents.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 400));
	}

    public void setUI(int index,Node node,ViewGroup view){
    	TextView mode=(TextView) view.findViewWithTag(""+(10*index+1));
    	TextView title=(TextView) view.findViewWithTag(""+(10*index+2));
    	TextView burb=(TextView) view.findViewWithTag(""+(10*index+3));
    	View line=(View) view.findViewWithTag(""+(10*index+4));     
    	
    	title.setText(node.getChildNodes().item(5).getTextContent());
    	burb.setText(node.getChildNodes().item(9).getTextContent());
    	Log.v("hello", "current "+current);
    	line.setBackgroundColor(TheNationMain.getColor(current, getContext()));

    	if(hilight){
    		if(index==1)
    			current=2;
    		else if(index==2)
    			current=12;
    		else if(index==3)
    			current=6;
    		else if(index==4)
    			current=15;
	    	mode.setText("");
	    	mode.setText(node.getChildNodes().item(1).getTextContent());
	    	mode.setTextColor(TheNationMain.getColor(current, getContext()));
	    	mode.setVisibility(View.VISIBLE);
	    	line.setBackgroundColor(TheNationMain.getColor(current, getContext()));
    	}
    }
    public void hilightFunction(){
    	if(tmpList==null)
    		tmpList=new Node[4];
    	hilightCall++;
    	if(hilightCall<=4){
    		
	        String[] urltop = getResources().getStringArray(R.array.url_top);
    		if(hilightCall==1)
        		url=urltop[12];
    		else if(hilightCall==2)
        		url=urltop[6];
    		else if(hilightCall==3)
        		url=urltop[15];

			LongOperation operate = new LongOperation(); 
			operate.execute("hello3");
			Log.v("hello", ""+list.item(1).getChildNodes().item(5).getTextContent());
			
			if(hilightPage==null){
		        LayoutInflater layoutInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		        hilightPage = layoutInflater.inflate(R.layout.contentdetail, null);
	    		this.addPage(hilightPage);
			}
			Log.v("hello", "hilightCall "+hilightCall);
    		setUI(hilightCall, list.item(1), (ViewGroup) hilightPage);
    		tmpList[index]=list.item(1);
    		index++;
    	}
    }
    public void receiveData(){
    	if(hilight){
    		hilightFunction();
    		return;
    	}
		RootScroll main=(RootScroll)((Activity)con).findViewById(R.id.bigscroll);
		current=main.getCurrentPage();
		for(int i=0;i<list.getLength()-8;i+=8){
	        LayoutInflater layoutInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    		View pageView = layoutInflater.inflate(R.layout.contentdetail, null);
    		this.addPage(pageView);
    		setUI(1, list.item(i+1), (ViewGroup) pageView);
    		setUI(2, list.item(i+3), (ViewGroup) pageView);
    		setUI(3, list.item(i+5), (ViewGroup) pageView);
    		setUI(4, list.item(i+7), (ViewGroup) pageView);
		}
    	Log.v("hello", "set "+getHeight());
    }
    
    public boolean onTouch(View v, MotionEvent event) {
    	boolean value= super.onTouch(v, event);
		if(event.getAction()==MotionEvent.ACTION_UP&&!isMove){
			int page=getCurrentPage();
			int index=0;
			if(event.getX()>v.getWidth()/2)
				index+=1;
			if(event.getY()>v.getHeight()/2)
				index+=2;
			Log.v("hello", "obj index "+(page*4+index));
			Log.v("hello", "click "+((page*4+index)*2));
			TheNationMain main=(TheNationMain) getContext();
			if(hilight)
				main.clickNews(tmpList[index]); 
			else
				main.clickNews(list.item((page*4+index)*2+1));
		}
		return value;
	}
}
